package com.axend.aerosense.room.viewmodel;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.impl.IOperationDetailResult;
import com.axend.aerosense.common.bean.UserIconBean;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.HeadIconUploadService;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.services.ILoginService;
import com.axend.aerosense.common.services.config.ServicesConfig;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.entity.WardInfoBean;
import com.axend.aerosense.room.entity.WardEditBean;
import com.axend.aerosense.room.entity.WardGetInfo;

import io.reactivex.disposables.Disposable;

public class WardInfoViewModel extends CustomBaseViewModel<WardInfoBean> {
    public MutableLiveData<Boolean> edit = new MutableLiveData<>(false);
    public MutableLiveData<Boolean> editHeadImage = new MutableLiveData<>(false);
    public MutableLiveData<WardInfoBean> wardInfo = new MutableLiveData<>(new WardInfoBean());
    private IOperationResult operationResultListence = null;
    private Disposable disposable;

    @Autowired(name = ServicesConfig.User.LONGING_SERVICE)
    ILoginService iLoginService;

    public void setValue(WardInfoBean bean){
        if (bean.getWardBirthday() == null || bean.getWardBirthday().toLowerCase().equals("null")){
            bean.setWardBirthday("");
        }
        if (bean.getWardHeight() == null || bean.getWardHeight().toLowerCase().equals("null")){
            bean.setWardHeight("");
        }
        wardInfo.setValue(bean);
    }

    public WardInfoViewModel(IBaseView baseViewImpl, ILoadDataResult<WardInfoBean> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
        ARouter.getInstance().inject(this);
    }

    public void setOnOpresionListence(IOperationResult result){
        operationResultListence = result;
    }

    public void onAddWard(Activity activity, Bitmap upBitmap, IOperationDetailResult<WardInfoBean> operationDetailResult){
        if(TextUtils.isEmpty(wardInfo.getValue().getWardName())){
            ToastUtils.showShort(R.string.common_check_usename);
            return;
        }
        wardInfo.getValue().setWardImg(HeadIconUploadService.bitmapToBase64(upBitmap));
        disposable = EasyHttp.post(UrlApi.URL.AddWard.getUrl())
                .headers("token", iLoginService.getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(wardInfo.getValue()))
                .execute(new ProgressDialogCallBack<WardInfoBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        ToastUtils.showShort(e.getMessage());
                        if(null != operationDetailResult){
                            operationDetailResult.onFail(0, "");
                        }
                    }

                    @Override
                    public void onSuccess(WardInfoBean bean) {
                        dismissProgress();
                        if(null != operationDetailResult){
                            operationDetailResult.onSuccess(bean);
                        }
                    }
                });
    }

    public void onDeletWard(Activity activity, String uid){
        WardEditBean bean = new WardEditBean(uid);
        disposable = EasyHttp.post(UrlApi.URL.DelWard.getUrl())
                .headers("token", iLoginService.getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<WardInfoBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        ToastUtils.showShort(e.getMessage());
                        if(null != operationResultListence){
                            operationResultListence.onFail();
                        }
                    }

                    @Override
                    public void onSuccess(WardInfoBean bean) {
                        dismissProgress();
                        //此处目前是为一个废对象不做处理的
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.WARD_COUNT_CHANGE ,Integer.class).postValue(1);
                        ToastUtils.showShort(Utils.getApp().getString(R.string.common_success));
                        if(null != operationResultListence){
                            operationResultListence.onSuccess();
                        }
                    }
                });
    }

    public void getWard(Activity activity, String uid){
        WardEditBean bean = new WardEditBean(uid);
        disposable = EasyHttp.post(UrlApi.URL.GetWard.getUrl())
                .headers("token", iLoginService.getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new SimpleCallBack<WardGetInfo>() {
                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        loadDataFail(e.getMessage());
                    }

                    @Override
                    public void onSuccess(WardGetInfo bean) {
                        loadDataSuccess(bean.wardInfo);
                    }
                });
    }

    public void updateWard(Activity activity, String uid, IOperationResult operationResult){
        if(TextUtils.isEmpty(wardInfo.getValue().getWardName())){
            ToastUtils.showShort(R.string.common_check_usename);
            return;
        }
//        WardInfoBean bean = new WardInfoBean(uid, name.getValue(), phone.getValue(), sex.getValue(), birthDay.getValue(), hight.getValue(), weight.getValue());
        disposable = EasyHttp.post(UrlApi.URL.UpdateWard.getUrl())
                .headers("token", iLoginService.getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(wardInfo.getValue()))
                .execute(new ProgressDialogCallBack<WardInfoBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        ToastUtils.showShort(e.getMessage());
                        if(null != operationResult){
                            operationResult.onFail();
                        }
                    }

                    @Override
                    public void onSuccess(WardInfoBean bean) {
                        dismissProgress();
                        //此处目前是为一个废对象不做处理的
                        ToastUtils.showShort(Utils.getApp().getString(R.string.common_success));
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.WARD_COUNT_CHANGE ,Integer.class).postValue(1);
                        if(null != operationResult){
                            operationResult.onSuccess();
                        }
                    }
                });
    }
    @Override
    protected void onCleared() {
        if (null != disposable && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        super.onCleared();
    }

    @Override
    public void onLoad() {
    }
}
